package cn.b.sky.user.service.impl;

import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.common.utils.TreeUtil;
import cn.b.sky.user.dao.MenuDao;
import cn.b.sky.user.service.MenuService;
import cn.b.sky.user.service.model.Menu;
import cn.b.sky.user.service.model.condition.MenuCondition;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * 
 * @ClassName: MenuServiceImpl 
 * @Description:菜单管理业务实现类
 * @author: ZhaoXin
 * @date: 2015-7-29 上午10:43:48
 * @version: V1.0  
 *
 */
@Service("MenuService")
public class MenuServiceImpl implements MenuService {

  private static final Logger log= LoggerFactory.getLogger(MenuServiceImpl.class);

  @Autowired
  private MenuDao menuDao;

  @Override
  public List<TreeInterface> selectMenuList(String siteCode,boolean isContainLock) {
    List<Menu> menus = menuDao.selectMenuList(siteCode,isContainLock);
    return TreeUtil.gradingRecursion(menus.toArray(new Menu[menus.size()]), null);

  }

  @Override
  public Menu selectMenuByCode(String siteCode,String menuCode) {
    return menuDao.selectMenuByCode(siteCode,menuCode);
  }

  @Override
  public void insertMenu(Menu menu) {
    if (menu != null) {
      menuDao.insertMenu(menu);
    }

  }

  @Override
  public void updateMenu(Menu menu) {
    if (menu != null) {
      menuDao.updateMenu(menu);
    }

  }

  @Override
  public int selectMaxOrdinal(String siteCode,String parentMenuCode) {
    return menuDao.selectMaxOrdinal(siteCode,parentMenuCode);
  }

  @Override
  public void deleteMenu(String siteCode,String menuCode) {
    try {
      if (!StringUtils.isEmpty(menuCode)) {
        menuDao.deleteMenuTL(siteCode, menuCode);
        menuDao.deleteMenu(siteCode,menuCode);
      }
    } catch (Exception e) {
      throw ImeException.newException("menuCannotDel.M",e);
    }

  }

  @Override
  public int selectRootMaxOrdinal(String siteCode) {
    return menuDao.selectRootMaxOrdinal(siteCode);
  }

  @Override
  public List<Menu> selectMenuByParent(String siteCode,String parentMenuCode, boolean isContainLock) {
    return menuDao.selectMenuByParent(siteCode,parentMenuCode, isContainLock);
  }

  @Override
  public List<Menu> selectRootMenu(String siteCode,boolean isContainLock) {
    return menuDao.selectRootMenu(siteCode,isContainLock);
  }

  @Override
  public List<Menu> getCurrentUserMenus(String userName, String language) {
    if (StringUtils.isBlank(userName)) {
      log.error("当前用户为空!");
      return null;
    }
    return menuDao.selectMenuByUserName(userName, language);
  }

  @Override
  public List<TreeInterface> gradingRecursion(Menu[] menus, String parentid) {
    if (parentid == null) {
      parentid = StringUtils.EMPTY;
    }
    List<TreeInterface> parent = new ArrayList<TreeInterface>();
    List<TreeInterface> children = new ArrayList<TreeInterface>();
    for (Menu menu : menus) {
      if (menu.getIsLeaf()) {// form节点
        children.add(menu);
      } else {//非form节点
        parent.add(menu);
      }
    }
    if (children.size() == 0) {// 无子，孤独终老
      return parent;
    }
    // 有子，传宗接代，直到断子绝孙
    Set<TreeInterface> menuSet = new TreeSet<TreeInterface>();

    for (TreeInterface menu : children) {
      menuSet.add(menu);
      menuSet.addAll(getParentMuneSet(parent, menu.getPid()));
    }
    return TreeUtil.gradingRecursion(menuSet.toArray(new Menu[menuSet.size()]), null);
  }

  /**
   * 
   * <strong>Title : getParentMuneSet<br></strong>
   * <strong>Description : </strong>递归找出父节点<br> 
   * <strong>Create on : 2015-11-26 下午4:39:11<br></strong>
   * <p>
   * @param menus
   * @param pid
   * @return
   * Set<TreeInterface>
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private Set<TreeInterface> getParentMuneSet(List<TreeInterface> menus, String pid) {
    Set<TreeInterface> menuSet = new TreeSet<TreeInterface>();
    for (TreeInterface menu : menus) {
      if (menu.getNodeId().equals(pid)) {
        menuSet.add(menu);
        menuSet.addAll(this.getParentMuneSet(menus, menu.getPid()));
        break;
      }
    }
    return menuSet;
  }

  @Override
  public List<Menu> selectTypeMenuListByCondition(MenuCondition condition) {
    if (condition!=null) {
      return menuDao.selectTypeMenuListByCondition(condition);
    }
    return null;
  }

  @Override
  public int selectTypeMenuCountByCondition(MenuCondition condition) {
    if (condition!=null) {
      return menuDao.selectTypeMenuCountByCondition(condition);
    }
    return 0;
  }

  @Override
  public List<Menu> getCurrentSiteMenus(String siteCode, String currentLanguage) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(currentLanguage)) {
      log.error("工厂和语言为空!");
      return null;
    }
    return menuDao.selectMenuBySiteCode(siteCode, currentLanguage);
  }

}
